<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分类管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="30">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="type" @keyup.enter.native="search">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加分类</el-button>
        </el-col>
      </el-row>
      <el-table :default-sort = "{prop: 'id', order: 'descending'}" :data="perPage_data" stripe>
        <el-table-column sortable label="序号" prop="id"></el-table-column>
        <el-table-column label="分类名称" prop="type"></el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="scope">
            <el-button type="primary" icon="el-icon-edit" @click="open_edit_type(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="deletetype(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
     <pageination
       :total-data="total_data"
       @pageBar="handleChangePage"
       :layout="'total,prev,pager,next'"
       :pagesize="pagesize"
     ></pageination>
    </el-card>
    <el-dialog
        title="添加分类"
        :visible.sync="addDialogVisible"
        @close="addDialogClosed"
        width="50%">
        <!--内容主体区域-->
        <el-form :model="typeForm" :rules="typeRules" ref="typeFormRef">
            <el-form-item label="分类名称" prop="typeName">
              <el-input v-model="typeForm.typeName" @keyup.enter.native="addType"></el-input>
            </el-form-item>
        </el-form>
        <!--内容底部区域-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addType()">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog
        title="修改分类"
        :visible.sync="editDialogVisible"
        @close="editDialogClosed"
        width="50%">
        <!--内容主体区域-->
        <el-form :model="edittypeForm" :rules="edittypeRules" ref="edittypeFormRef">
            <el-form-item label="分类名称" prop="typeName">
              <el-input v-model="edittypeForm.typeName" @keyup.enter.native="edit_type"></el-input>
            </el-form-item>
        </el-form>
        <!--内容底部区域-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="edit_type()">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import pageination from '@/components/pageination'
import { MessageBox } from 'element-ui'
export default {
  components: {
    pageination
  },
  data () {
    return {
      cid: '',
      typelist: [],
      perPage_data: [],
      currentPage: 1,
      total_data: 10,
      pagesize: 9,
      addDialogVisible: false,
      editDialogVisible: false,
      typeForm: {
        typeName: ''
      },
      typeRules: {
        typeName: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      },
      edittypeForm: {
        id: '',
        typeName: ''
      },
      edittypeRules: {
        typeName: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      },
      type: ''
    }
  },
  created () {
    this.getTypes()
  },
  methods: {
    handleChangePage (val) {
      this.$axios({
        url: '/getCate',
        data: val,
        method: 'post'
      }).then(response => {
        console.log(response.data)
        this.perPage_data = this.typelist.slice((val.pageCurrent - 1) * this.pagesize, val.pageCurrent * this.pagesize)
      }).catch(error => {
        console.log(error)
      })
    },
    open_edit_type (id) {
      this.editDialogVisible = true
      this.cid = id
    },
    edit_type () {
      this.$axios({
        url: '/updateCate/' + this.cid + '/' + this.edittypeForm.typeName,
        method: 'put'
      }).then(response => {
        console.log(response)
        if (response.data !== 1) this.$message.error('修改失败')
        else {
          this.$message.success('修改成功')
          this.editDialogVisible = false
          this.$router.go(0)
        }
      })
    },
    addDialogClosed () {
      this.$refs.typeFormRef.resetFields()
    },
    editDialogClosed () {
      this.$refs.edittypeFormRef.resetFields()
    },
    getTypes () {
      this.$axios({
        url: '/getCate',
        method: 'post',
        data: {}
      }).then(response => {
        console.log(response.data)
        for (let i = 0; i < response.data.length; i++) {
          var type = {
            id: response.data[i].cid,
            type: response.data[i].category
          }
          this.typelist.push(type)
        }
        this.perPage_data = this.typelist.slice(0, this.pagesize)
        this.total_data = response.data.length
      }).catch(error => {
        console.log(error)
      })
    },
    deletetype (id) {
      MessageBox.confirm('确认删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios({
          url: '/deleteCate/' + id,
          method: 'delete'
        }).then(response => {
          if (response.data !== 1) this.$message.error('删除失败')
          else this.$message.success('删除成功')
          this.$router.go(0)
        })
      })
    },
    addType () {
      this.$axios({
        url: '/insertCate/' + this.typeForm.typeName,
        method: 'post'
      }).then(response => {
        console.log(response)
        if (response.data !== 1) this.$message.error('添加失败')
        else this.$message.success('添加成功')
        this.$router.go(0)
      })
      this.addDialogVisible = false
    },
    search () {
      if (this.type !== '') {
        this.$axios({
          method: 'get',
          url: '/getCates/' + this.type
        }).then(response => {
          console.log(response)
          this.typelist = []
          for (let i = 0; i < response.data.length; i++) {
            var type = {
              id: response.data[i].cid,
              type: response.data[i].category
            }
            this.typelist.push(type)
          }
          this.perPage_data = this.typelist.slice(0, this.pagesize)
          this.total_data = response.data.length
        })
      } else {
        this.typelist = []
        this.getTypes()
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
